/*
 * GeoArticle main script
 *
 * Copyright 2010 Ivar Bergman (http://ivarbergman.se)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

if (window.console == undefined)
  {
    window.console = {
      log: function() { }
    };
  }

/*
var GAConf =
  {
    divHookID: "#map",
    language : "sv",
    lng : 59.77,
    lat : 13.466896,
    zoom:  12,
    maxZoom : 20,
    minZoom : 0,
    moduleBase: "http://stage/joomla/modules/mod_geoarticle/",
    joomlaBase: "http://stage/joomla/"
  };
*/
jQuery(document).ready(function()
  {
    GAApp.init();
  });


var GA = null;
var GAApp =
  {
    category: {},
    mapUI: null,
    navigationUI: null,
    init: function()
    {
      GAConf.home = new GLatLng(GAConf.lng,GAConf.lat);
      GAConf.urlData = GAConf.joomlaBase + "index.php?option=com_geoarticle&task=geotags&format=raw&lang="+GAConf.language;
      GAConf.urlArticle = GAConf.joomlaBase + "index.php?option=com_content&view=article&id=#cid#&tmpl=article&lang="+GAConf.language;
      GAConf.urlPrint = GAConf.joomlaBase + "index.php?option=com_content&view=article&id=#cid#&tmpl=article&print=1&lang="+GAConf.language;
      GAConf.urlPrintMap = GAConf.joomlaBase + "index.php?option=com_content&view=article&id=#cid#&tmpl=article&print=1&lang="+GAConf.language;
      GAConf.skinBase = GAConf.moduleBase + "tmpl/skin/"+GAConf.skin+"/";
      GAConf.icon = GAConf.joomlaBase + "modules/mod_geoarticle/tmpl/skin/"+GAConf.skin+"/map_marker_medium_vertical.png";

      GAConf.standard = (GAConf.skin == 'google');

      // Init the UI components
      GA = $(GAConf.divHookID);

      // Load the GeoArticle data from the Joomla component.
      $.ajax({
	       async:true,
	       url: GAConf.urlData + "&ts=" + Date.UTC(),
	       success: function(data, textStatus, XMLHttpRequest) {
		 this.mapUI = new GAMapUI();
		 this.navigationUI = new GANavigationUI();
		 for (var cid in data.categories)
		 {
		   var name =  data.categories[cid];
		   GAApp.addCategory(cid, name);;
		 }
		 for (var idx = 0; idx < data.articles.length; idx++)
		 {
		   var a =  data.articles[idx];
		   GAApp.addMarker(a.id, a.categories, a.title, a.long , a.lat);
		 }
		 this.navigationUI.showDefaultCategories();
	       },
	       dataType: "json",
	       error: function(XMLHttpRequest, textStatus, errorThrown)
	       {
	       }
	});
      this.mapType("normal");
      this.center();
    },

    showCategory: function(id)
    {
      console.log(id);
      this.category[id].show();
    },
    hideCategory: function(id)
    {
      this.category[id].hide();
    },
    addCategory: function(id, title)
    {
      this.category[id] = new ArticleCategory(id, title);;
    },
    addMarker: function(id, categories, title, lat, lng)
    {
      if (!categories)
	{
	  return ;
	}

      // If skin is non standard/google create a marker icon.
      if (!GAConf.standard)
	{
	  var icon = new GIcon();
	  icon.image = GAConf.skinBase + "map_marker_medium_vertical.png";
	  icon.iconAnchor = new GPoint(14, 36);
	  icon.infoWindowAnchor = new GPoint(25, 7);
	  icon.iconSize = new GSize(26, 40);
	}

      // Create the marker
      var marker = new ArticleMarker(lat, lng, id, title, categories, icon);

      // Add the marker to the categories
      for (var idx = 0 ; idx< categories.length; idx++)
	{
	  var i = categories[idx];
	  this.category[i].markers.push(marker);
	}
    },

    mapType :function(type)
    {
      GA.trigger("mapType", type);
    },

    zoom :function(value)
    {
      GA.trigger("zoom", value);
    },

    center :function()
    {
      GA.trigger("center");
    },

    closeArticle :function()
    {
      GA.trigger("closeArticle");
    }


  };


/* --------------------------------------------------------------------------- */
/* Util functions and jquery extensions                                        */
/* --------------------------------------------------------------------------- */
function array_indexOf(arr, obj) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == obj)
      {
	return i;
      }
  }
  return -1;
};

function array_has(arr, obj) {
  return (array_indexOf(arr, obj) >= 0);
};



function array_size(arr) {
  var len = 0;
    for (var k in arr)
      if (arr.hasOwnProperty(k))
	len++;
  return len;
};

(function($) {

   $.getScripts = function(scripts, onComplete)
   {
     var i = 1;
     var ii = scripts.length;
     var onScriptLoaded = function(data, response) { if (i++ == ii) onComplete(); } ;
     for(var s in scripts) { $.getScript(scripts[s], onScriptLoaded); } ;
   };

   $.fn.aOp = function() {
    var op = this.attr("href");
    if (op.indexOf("#") > -1)
    {
      op = op.substr(op.indexOf("#")+1);
    }
    return op;
  };
 })(jQuery);

